package com.server.modules.api.web;

import com.server.common.utils.StringUtils;
import com.server.modules.api.Base.BaseAppController;
import com.server.modules.api.utils.Constants;
import com.server.modules.api.vo.BaseQueryVo;
import com.server.modules.api.vo.FriendQueryVO;
import com.server.modules.api.vo.FriendVO;
import com.server.modules.sys.entity.User;
import com.server.modules.sys.entity.friends.Friends;
import com.server.modules.sys.entity.member.Member;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/2.
 */
@Controller
@RequestMapping(value = "/api/friends")
public class FriendAPIController extends BaseAppController {

/*
    @ResponseBody
    @RequestMapping(value = "/sendRequest", method = RequestMethod.POST)
    public BaseQueryVo sendRequest(HttpServletRequest request,FriendVO vo) throws Exception{
        if (!checkLogin(vo)){
            return returnMsgAPIService.getRet(Constants.errLogin);
        }
        Friends friends = new Friends();
        friends.setFriendAId(vo.getMemberId());
        friends.setFriendAType("1");
        friends.setFriendBId(vo.getHxName());
        friends.setFriendBType(vo.getType());
        friends.setDelFlag("2"); //审核中
        BaseQueryVo baseQueryVo = new BaseQueryVo();
        String hxUserNameA = iFriendsService.getUserHxUserName(friends.getFriendAId(),friends.getFriendAType());
        String hxUserNameB = iFriendsService.getUserHxUserName(friends.getFriendBId(),friends.getFriendBType());
        if (!StringUtils.isNotEmpty(hxUserNameA)){
            return returnMsgAPIService.getRet(Constants.notFoutUser);
        }
        if (!StringUtils.isNotEmpty(hxUserNameB) ){
            return returnMsgAPIService.getRet(Constants.notFoutUser);
        }
        if (iFriendsService.checkFriends(friends)){
            return returnMsgAPIService.getRet(Constants.isFrined);
        }
        Member member = memberAPIService.selectMemberById(vo.getMemberId());
        List<String> target = new ArrayList<String>();
        target.add(hxUserNameB);
        Map msg = new HashMap();
        msg.put("type", "txt");
        if (vo.getReason() != null){
            msg.put("msg",vo.getReason());
        }else{
            msg.put("msg", member.getTelephone() + " 想添加你为好友！");
        }
        Map map =  imService.sendMessages("users", target, msg, member.getTelephone(), null);
        if (map != null){
            iFriendsService.addFriends(friends);
            return returnMsgAPIService.getRet(0);
        }else {
            baseQueryVo.setRetCode(Constants.addFriendFailse);
            baseQueryVo.setRetMsg("添加好友失败");
            return baseQueryVo;
        }
    }
    @ResponseBody
    @RequestMapping(value = "/getRequest", method = RequestMethod.POST)
    public BaseQueryVo getRequest(HttpServletRequest request,FriendVO vo) throws Exception{
        if (!checkLogin(vo)){
            return returnMsgAPIService.getRet(Constants.errLogin);
        }
        BaseQueryVo<List> baseQueryVo = new BaseQueryVo<List>();
        Member member = new Member();
        member.setId(vo.getMemberId());
        member.setDelFlag("2");//2为请求未审核的好友
        List<FriendQueryVO> list = iFriendsService.getFriendsList(member);
        baseQueryVo.setRetCode(0);
        baseQueryVo.setRetMsg("查询成功");
        baseQueryVo.setData(list);
        return baseQueryVo;
    }
*/
    @ResponseBody
    @RequestMapping(value = "/allowAddFriend", method = RequestMethod.POST)
    public BaseQueryVo allowAddFriend(HttpServletRequest request,FriendVO vo) throws Exception{
        try {
            if (!checkLogin(vo)) {
                return returnMsgAPIService.getRet(Constants.errLogin);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return returnMsgAPIService.getRet(Constants.errLogin);
        }
        if (StringUtils.isEmpty( vo.getHxName())){
            return returnMsgAPIService.getRet(Constants.incompleteData);
        }
        Friends friends = new Friends();
        friends.setFriendAId(vo.getMemberId());
        friends.setFriendAType("1");
        BaseQueryVo baseQueryVo = new BaseQueryVo();
        Member member = memberAPIService.getByHxUsername(vo.getHxName());
        if (member!=null){
            friends.setFriendBId(member.getId());
            if (vo.getMemberId().equals(member.getId())){
                return returnMsgAPIService.getRet(Constants.addFriendFailse);
            }
        }
        friends.setFriendBType("1");
        String hxUserNameA = memberService.get(vo.getMemberId()).getHxUsername();
        if (!StringUtils.isNotEmpty(hxUserNameA) ){
            return returnMsgAPIService.getRet(Constants.notFoutUser);
        }
        if (iFriendsService.checkFriends(friends)){
            return returnMsgAPIService.getRet(Constants.isFrined);
        }
        //imService.equals();
        Map map =  imService.addFriendSingle(hxUserNameA,vo.getHxName());
        if (map != null){
            return returnMsgAPIService.getRet(0);
        }else {
            baseQueryVo.setRetCode(Constants.addFriendFailse);
            baseQueryVo.setRetMsg("添加好友失败");
            return baseQueryVo;
        }
    }
    @ResponseBody
    @RequestMapping(value = "/delFriend", method = RequestMethod.POST)
    public BaseQueryVo delFriend(HttpServletRequest request,FriendVO vo) throws Exception{
        try {
            if (!checkLogin(vo)) {
                return returnMsgAPIService.getRet(Constants.errLogin);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return returnMsgAPIService.getRet(Constants.errLogin);
        }
        if (StringUtils.isEmpty( vo.getHxName())){
            return returnMsgAPIService.getRet(Constants.incompleteData);
        }
        Friends friends = new Friends();
        friends.setFriendAId(vo.getMemberId());
        friends.setFriendAType("1");
        Member member = memberAPIService.getByHxUsername(vo.getHxName());
        if (member!=null){
            friends.setFriendBId(member.getId());
        }
        friends.setFriendBType("1");
        BaseQueryVo baseQueryVo = new BaseQueryVo();
        String hxUserNameA = memberService.get(vo.getMemberId()).getHxUsername();
        if (!StringUtils.isNotEmpty(hxUserNameA) ){
            return returnMsgAPIService.getRet(Constants.notFoutUser);
        }
        if (!iFriendsService.checkFriends(friends)){
            return returnMsgAPIService.getRet(Constants.isNotFrined);
        }
        Map map =  imService.deleteFriendSingle(hxUserNameA,vo.getHxName());
        if (map != null){
            iFriendsService.delFriends(friends);
            return returnMsgAPIService.getRet(0);
        }else {
            baseQueryVo.setRetCode(Constants.addFriendFailse);
            baseQueryVo.setRetMsg("删除好友失败");
            return baseQueryVo;
        }
    }
    @ResponseBody
    @RequestMapping(value = "/getFriends", method = RequestMethod.POST)
    public BaseQueryVo getFriends(HttpServletRequest request,FriendVO vo) throws Exception{
        try {
            if (!checkLogin(vo)) {
                return returnMsgAPIService.getRet(Constants.errLogin);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return returnMsgAPIService.getRet(Constants.errLogin);
        }
        BaseQueryVo<List> baseQueryVo = new BaseQueryVo<List>();
        Member member = new Member();
        member.setId(vo.getMemberId());
        member.setDelFlag("0");
        List<FriendQueryVO> list = iFriendsService.getFriendsList(member); //0为已是好友
        baseQueryVo.setRetCode(0);
        baseQueryVo.setRetMsg("查询成功");
        baseQueryVo.setData(list);
         return baseQueryVo;
    }

    @ResponseBody
    @RequestMapping(value = "/getFriendByPhoneNum", method = RequestMethod.POST)
    public BaseQueryVo getFriendByPhoneNum(HttpServletRequest request,FriendVO vo) throws Exception{
        try {
            if (!checkLogin(vo)) {
                return returnMsgAPIService.getRet(Constants.errLogin);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return returnMsgAPIService.getRet(Constants.errLogin);
        }
        BaseQueryVo<Member> baseQueryVo = new BaseQueryVo<Member>();
        Member member = memberAPIService.getMemberByPhoneNum(vo.getPhoneNum());
        if (member!=null){
            Member newMember = new Member();
            newMember.setNickname(member.getNickname());
            newMember.setHxUsername(member.getHxUsername());
            newMember.setFace(member.getFace());
            newMember.setSex(member.getSex());
            baseQueryVo.setRetCode(0);
            baseQueryVo.setRetMsg("查询成功");
            baseQueryVo.setData(newMember);
            return baseQueryVo;
        }else {
            return  returnMsgAPIService.getRet(Constants.notFound);
        }
    }
    @ResponseBody
    @RequestMapping(value = "/getManagerFirstAsk", method = RequestMethod.POST)
    public BaseQueryVo getManagerFirstAsk(HttpServletRequest request,FriendQueryVO vo){
        try {
            if (!checkLogin(vo)){
                return returnMsgAPIService.getRet(Constants.errLogin);
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            return returnMsgAPIService.getRet(Constants.errLogin);
        }
        User user = userAPIService.getUserByHxId(vo.getHxUserName());
        Member member = memberService.get(vo.getMemberId());
        if (user!=null){
            List<String> userList = new ArrayList<String>(1);
            if (StringUtils.isNotEmpty(member.getHxUsername())){
                userList.add(member.getHxUsername());
                Map<String,String > msg = new HashedMap();
                Map<String,String > ext = new HashedMap();
                msg.put("type","text");
                msg.put("msg","亲，有什么可以帮助你的么？");
                imService.sendMessages("users",userList,msg,vo.getHxUserName(),ext);
                imService.addFriendSingle(member.getHxUsername(),member.getHxUsername()); //
                return returnMsgAPIService.getRet(Constants.ok);
            }else  return returnMsgAPIService.getRet(Constants.notFoutUser);
        }else {
            return returnMsgAPIService.getRet(Constants.notFoutUser);
        }
    }
    @ResponseBody
    @RequestMapping(value = "/getFriendInfo", method = RequestMethod.POST)
    public BaseQueryVo getFriendInfo(HttpServletRequest request,FriendQueryVO vo){
        try {
            if (!checkLogin(vo)){
                return returnMsgAPIService.getRet(Constants.errLogin);
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            return returnMsgAPIService.getRet(Constants.errLogin);
        }
        Member member = imService.getFriendInfo(vo.getHxUserName());
        if (member!=null){
            BaseQueryVo baseQueryVo = new BaseQueryVo();
            Member tmpMember = new Member();
            tmpMember.setFace(member.getFace());
            tmpMember.setSex(member.getSex());
            tmpMember.setNickname(member.getNickname());
            tmpMember.setNation(member.getNation());
            tmpMember.setBirthday(member.getBirthday());
            baseQueryVo.setData(tmpMember);
            baseQueryVo.setRetCode(0);
            baseQueryVo.setRetMsg("查询成功");
            return baseQueryVo;
        }else  return returnMsgAPIService.getRet(Constants.notFoutUser);
    }

}
